<html>
<head><meta charset="utf-8"><title>string literal as bytes · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/string.20literal.20as.20bytes.html">string literal as bytes</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216039910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/string%20literal%20as%20bytes/near/216039910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/string.20literal.20as.20bytes.html#216039910">(Nov 09 2020 at 03:06)</a>:</h4>
<p>I was looking at the string literal as bytes lint test, and I felt confused by part of it. Here's the whole file:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// run-rustfix</span>

<span class="cp">#![allow(dead_code, unused_variables)]</span><span class="w"></span>
<span class="cp">#![warn(clippy::string_lit_as_bytes)]</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">str_lit_as_bytes</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">bs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"hello there"</span><span class="p">.</span><span class="n">as_bytes</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">bs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">r###"raw string with 3# plus " ""###</span><span class="p">.</span><span class="n">as_bytes</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="c1">// no warning, because these cannot be written as byte string literals:</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ubs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"☃"</span><span class="p">.</span><span class="n">as_bytes</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ubs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"hello there! this is a very long string"</span><span class="p">.</span><span class="n">as_bytes</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">strify</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stringify</span><span class="o">!</span><span class="p">(</span><span class="n">foobar</span><span class="p">).</span><span class="n">as_bytes</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">current_version</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">env</span><span class="o">!</span><span class="p">(</span><span class="s">"CARGO_PKG_VERSION"</span><span class="p">).</span><span class="n">as_bytes</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">includestr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">include_str</span><span class="o">!</span><span class="p">(</span><span class="s">"entry_unfixable.rs"</span><span class="p">).</span><span class="n">as_bytes</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"string with newline</span><span class="se">\t\n</span><span class="s">"</span><span class="p">.</span><span class="n">as_bytes</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>Why can these not be converted to byte string literals?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="c1">// no warning, because these cannot be written as byte string literals:</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ubs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"☃"</span><span class="p">.</span><span class="n">as_bytes</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ubs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"hello there! this is a very long string"</span><span class="p">.</span><span class="n">as_bytes</span><span class="p">();</span><span class="w"></span>
</code></pre></div>
<p>I tried adding a <code>b</code> before the second string literal on the Rust playground, and it compiled fine.</p>



<a name="216043537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/string%20literal%20as%20bytes/near/216043537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Wright <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/string.20literal.20as.20bytes.html#216043537">(Nov 09 2020 at 04:41)</a>:</h4>
<p>git can help you figure these things out.</p>
<div class="codehilite"><pre><span></span><code>&gt; git blame -L 13,13 tests/ui/string_lit_as_bytes.rs
6d1aaac6c3 (Jens Hausdorf 2019-03-10 11:06:19 +0100 13)     let ubs = &quot;hello there! this is a very long string&quot;.as_bytes();
&gt; git show 6d1aaac6c3
commit 6d1aaac6c373e4781f779bddf992242abb53c7c3
Author: Jens Hausdorf &lt;mail@jens-hausdorf.de&gt;
Date:   Sun Mar 10 11:06:19 2019 +0100

    Avoid reporting string_lit_as_bytes for long strings

    Port of @jens1o code ([b76f939][jens1o_commit])

    Fixes #1208

    [jens1o_commit]: https://github.com/jens1o/rust-clippy/commit/b76f939ac2efcfe24900c286b3b7713d972d9088

    Co-authored-by: Thiago Arrais &lt;thiago.arrais@gmail.com&gt;
...
</code></pre></div>
<p><a href="https://github.com/rust-lang/rust-clippy/issues/1208">https://github.com/rust-lang/rust-clippy/issues/1208</a> shows why it was done. They put it in to avoid situation were the expression was immediately used by <code>as_ref</code>. </p>
<p>The problem is that <code>as_bytes()</code> gives <code>&amp;[u8]</code> while the literal syntax gives a <code>[u8]</code> and the two aren't the same.</p>



<a name="216124701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/string%20literal%20as%20bytes/near/216124701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/string.20literal.20as.20bytes.html#216124701">(Nov 09 2020 at 18:26)</a>:</h4>
<p>Ah, thanks :)</p>
<p>Hopefully const generics will fix this!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>